package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sc.nft.enums.PrizeDrawActivityPoolBonusTaypEnum;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.PrizeDrawActivityPool;
import com.sc.nft.mapper.PrizeDrawActivityPoolMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * 抽奖活动-瓜分活动-奖金池配置(sc_prize_draw_activity_pool)数据DAO
 *
 * @author Sun
 * @since 2023-09-14 14:05:39
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class PrizeDrawActivityPoolDao extends ServiceImpl<PrizeDrawActivityPoolMapper, PrizeDrawActivityPool> {

    public List<PrizeDrawActivityPool> getListByActivityId(Long activityId) {
        LambdaQueryWrapper<PrizeDrawActivityPool> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PrizeDrawActivityPool::getActivityId,activityId);
        return list(wrapper);
    }

    public List<PrizeDrawActivityPool> getOneByLinkIdAndBonusType(Long linkId, PrizeDrawActivityPoolBonusTaypEnum bonusType) {
        LambdaQueryWrapper<PrizeDrawActivityPool> wrapper = new LambdaQueryWrapper<>();
        if(Objects.nonNull(linkId)){
            wrapper.eq(PrizeDrawActivityPool::getLinkId,linkId);
        }
        wrapper.eq(PrizeDrawActivityPool::getBonusType,bonusType);
        return list(wrapper);
    }

    public Boolean addPoolAmountById(Long poolId, BigDecimal amount) {
        LambdaUpdateWrapper<PrizeDrawActivityPool> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(PrizeDrawActivityPool::getId,poolId);
        wrapper.setSql("amount = amount + " + amount);
        return  update(new PrizeDrawActivityPool(),wrapper);
    }

    public BigDecimal getSumAmountByActivityId(Long activityId) {
        return baseMapper.getSumAmountByActivityId(activityId);
    }

    public Integer getCountByActivityIdAndBonusTypeAndLinkId(Long activityId, PrizeDrawActivityPoolBonusTaypEnum bonusType, Long linkId) {
        LambdaQueryWrapper<PrizeDrawActivityPool> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PrizeDrawActivityPool::getActivityId,activityId);
        wrapper.eq(PrizeDrawActivityPool::getBonusType,bonusType);
        wrapper.eq(PrizeDrawActivityPool::getLinkId,linkId);
        return count(wrapper);
    }
}